package esb.chapter4.messageflow.service.util;

import java.io.ByteArrayOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import esb.chapter4.messageflow.domain.BookQuote;

public class QuoteMessageHelper {
	
	public static String marshallQuote(BookQuote bookQuote) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		DOMImplementation impl = builder.getDOMImplementation();
		Document quoteDocument = impl.createDocument(null, "book-quote", null);
		Element quoteElement = quoteDocument.getDocumentElement();
		Element isbnElement = quoteDocument.createElement("isbn");
		isbnElement.setTextContent(bookQuote.getIsbn());
		Element priceElement = quoteDocument.createElement("price");
		priceElement.setTextContent(bookQuote.getPrice().toString());
		Element storeElement = quoteDocument.createElement("bookstore");
		storeElement.setTextContent(bookQuote.getCompanyName());
		quoteElement.appendChild(isbnElement);
		quoteElement.appendChild(priceElement);
		quoteElement.appendChild(storeElement);
		DOMSource domSource = new DOMSource(quoteDocument);
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		StreamResult streamResult = new StreamResult(byteStream);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer serializer = tf.newTransformer();
		serializer.transform(domSource, streamResult);
		return byteStream.toString();
	}
	
	public static String marshallBarnesQuote(BookQuote bookQuote) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		DOMImplementation impl = builder.getDOMImplementation();
		Document quoteDocument = impl.createDocument(null, "book-information", null);
		Element quoteElement = quoteDocument.getDocumentElement();
		Attr isbnAttribute = quoteDocument.createAttribute("isbn");
		isbnAttribute.setValue(bookQuote.getIsbn());
		Element priceElement = quoteDocument.createElement("book-price");
		priceElement.setTextContent(bookQuote.getPrice().toString());
		Element storeElement = quoteDocument.createElement("store-name");
		storeElement.setTextContent(bookQuote.getCompanyName());
		quoteElement.setAttributeNode(isbnAttribute);
		quoteElement.appendChild(priceElement);
		quoteElement.appendChild(storeElement);
		DOMSource domSource = new DOMSource(quoteDocument);
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		StreamResult streamResult = new StreamResult(byteStream);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer serializer = tf.newTransformer();
		serializer.transform(domSource, streamResult);
		return byteStream.toString();
	}
	
	public static BookQuote unmarshallQuote(Node payloadElem) {
		NodeList childList = payloadElem.getChildNodes();
		BookQuote bookQuote = new BookQuote();
        for(int i = 0; i < childList.getLength(); i++) {
        	Node childNode = childList.item(i);
        	if("isbn".equals(childNode.getLocalName())) {
        		bookQuote.setIsbn(childNode.getTextContent());
        	} else if("price".equals(childNode.getLocalName())) {
        		bookQuote.setPrice(Float.valueOf(childNode.getTextContent()));
        	} else if("bookstore".equals(childNode.getLocalName())) {
        		bookQuote.setCompanyName(childNode.getTextContent());
        	}
        }
        return bookQuote;
	}

}
